package com.tg.app.helper;

import android.widget.CheckBox;
import android.widget.TextView;

import com.tange.core.device.facade.DeviceFacade;
import com.tange.core.device.facade.Instruction;
import com.tg.app.R;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.IOCtrlMsg;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;

import java.util.ArrayList;
import java.util.List;


public class AppHelper {
    private static final String QMSY_APP = "com.tange365.qmsy";
    private static final String DUOMIGE_APP = "com.tange365.duomige";
    private static final String WECAM_APP = "com.tange365.wecam";
    private static final String BUGUNIAO = ".buguniao";

    public static boolean isBuguniao(){
        String name = TGApplicationBase.getApplicationContext().getPackageName();
        TGLog.d(QMSY_APP, "name = " + name);
        return !StringUtils.isEmpty(name) && name.toLowerCase().contains(BUGUNIAO);
    }

    public static boolean isQmsyApp(){
        String name = TGApplicationBase.getApplicationContext().getPackageName();
        TGLog.d(QMSY_APP, "name = " + name);
        return !StringUtils.isEmpty(name) && name.toLowerCase().contains(QMSY_APP);
    }

    public static boolean isDuomigeApp(){
        String name = TGApplicationBase.getApplicationContext().getPackageName();
        TGLog.d(DUOMIGE_APP, "name = " + name);
        return !StringUtils.isEmpty(name) && name.toLowerCase().contains(DUOMIGE_APP);
    }

    public static void setDuoigeText(TextView textView, CheckBox checkBox, TextView tvTip){
        textView.setText(R.string.txt_light_flash_slowly);
        checkBox.setText(R.string.txt_light_flashing_slowly);
        tvTip.setText(R.string.txt_no_slow_flash);
    }

    public static boolean mustModify(List<String> list){
        if (!AppHelper.isQmsyApp()){
            return false;
        }

        if (list != null && list.size() > 1){
            List<String> arr = new ArrayList<>();
            arr.add(list.get(0));
            for (int i = 1; i < list.size(); i++){
                if (!arr.contains(list.get(i))){
                    arr.add(list.get(i));
                }
            }

            if (arr.size() ==  4 || arr.size() == 5){
                return false;
            }

            String str = list.get(list.size() - 1);
            String normal_definition = ResourcesUtil.getString(R.string.normal_definition);
            String sd = "sd";
            return StringUtils.equalsIgnoreCase(str, sd) || StringUtils.equalsIgnoreCase(normal_definition, str);
        }
        return true;
    }

    public static void getModifyList(List<String> list) {
        List<String> arr = new ArrayList<>();
        arr.add(list.get(0));
        int index = -1;
        for (int i = 1; i < list.size(); i++) {
            if (!arr.contains(list.get(i))) {
                arr.add(list.get(i));
            } else {
                index = i;
            }
        }

        int end = index == -1 ? -1 : 0;
        list.set(list.size() - 1, ResourcesUtil.getString(R.string.high_definition));

        for (int i = list.size() - 2; i > end; i--) {
            String str = list.get(i);
            if (StringUtils.equalsIgnoreCase(str, list.get(i + 1))) {
                if (StringUtils.equals(ResourcesUtil.getString(R.string.normal_definition), str)) {
                    str = ResourcesUtil.getString(R.string.high_definition);
                    list.set(i, str);
                } else if (StringUtils.equals(ResourcesUtil.getString(R.string.high_definition), list.get(i))) {
                    str = ResourcesUtil.getString(R.string.full_high_definition);
                    list.set(i, str);
                } else if (StringUtils.equals(ResourcesUtil.getString(R.string.full_high_definition), list.get(i))) {
                    str = ResourcesUtil.getString(R.string.full_high_definition_uhd);
                    list.set(i, str);
                }
            }
        }

        if (index != -1) {
            list.set(0, list.get(index));
        }
    }

    public static void sendIOCtrl(DeviceFacade deviceFacade, int ioType, byte[] ioCtrlBuf) {
        deviceFacade.getInstruct().sendInternal(new Instruction(ioType, ioCtrlBuf));
    }

    public static void sendPTZCMDShort(DeviceFacade deviceFacade, int contorl, int channel) {
        int x = 0;
        int y = 0;
        int z = 0;
        if (contorl == AVIOCTRLDEFs.AVIOCTRL_PTZ_UP) {
            y = 10;
        } else if (contorl == AVIOCTRLDEFs.AVIOCTRL_PTZ_DOWN) {
            y = -10;
        }
        if (contorl == AVIOCTRLDEFs.AVIOCTRL_PTZ_LEFT) {
            x = -5;
        }
        if (contorl == AVIOCTRLDEFs.AVIOCTRL_PTZ_RIGHT) {
            x = 5;
        }
        sendPTZCMDShort(deviceFacade, x, y, z,channel);
    }


    public static void sendPTZCMDShort(DeviceFacade deviceFacade, int x,int y,int z,int channel){
        sendIOCtrl(deviceFacade, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_PTZ_SHORT_COMMAND, AVIOCTRLDEFs.SMsgAVIoctrlPtzShortCmd.parseContent(x, y, z,channel));
    }

    public static void sendPTZCMD(DeviceFacade deviceFacade, int control,int ch) {
        byte contr = (byte) control;
        byte speed = 0;
        byte point = 0;
        byte limit = 0;
        byte aux = 0;
        byte[] reserve = new byte[2];
        byte channel = (byte) ch;
        sendIOCtrl(deviceFacade, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_PTZ_COMMAND, AVIOCTRLDEFs.SMsgAVIoctrlPtzCmd.parseContent(contr, speed, point, limit, aux, channel));
    }

    public static void sendPTZCMD(DeviceFacade deviceFacade, int control,int ch, byte point) {
        byte contr = (byte) control;
        byte speed = 0;
        byte limit = 0;
        byte aux = 0;
        byte[] reserve = new byte[2];
        byte channel = (byte) ch;
        sendIOCtrl(deviceFacade, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_PTZ_COMMAND, AVIOCTRLDEFs.SMsgAVIoctrlPtzCmd.parseContent(contr, speed, point, limit, aux, channel));
    }

    public static void getPrimaryViewCmd(DeviceFacade deviceFacade) {
        sendIOCtrl(deviceFacade, AVIOCTRLDEFs.TCI_CMD_GET_PRIMARY_VIEW_REQ, new byte[]{0});
    }

    public static void setPrimaryViewCmd(DeviceFacade deviceFacade, int channel) {
        sendIOCtrl(deviceFacade, AVIOCTRLDEFs.TCI_CMD_SET_PRIMARY_VIEW_REQ, AVIOCTRLDEFs.Tcis_SetPrimaryView.parseContent(channel));
    }

    public static void setStreamCtrlCMD(DeviceFacade deviceFacade, int channel,int type){
        sendIOCtrl(deviceFacade, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETSTREAMCTRL_REQ, AVIOCTRLDEFs.SMsgAVIoctrlSetStreamCtrlReq.parseContent(channel, (byte) type));
    }
}
